package com.lemon.cloud.admin.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.security.annotation.Inner;
import com.lemon.cloud.log.annotation.SysLog;
import com.lemon.cloud.comm.constants.enums.LogBusType;
import com.lemon.cloud.admin.entity.SysPublicParam;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.admin.service.SysPublicParamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@RestController
@RequiredArgsConstructor
@RequestMapping("/param")
@Tag(name = "公共参数配置表接口")
public class SysParamsController {

    @Autowired
    private SysPublicParamService sysPublicParamService;

    @Operation(
            summary = "获取公共参数",
            parameters = {@Parameter(name = "publicKey",description = "key值,询问后端开发人员",required = true,in = ParameterIn.PATH)},
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @Inner(value = false)
    @GetMapping("/publicValue/{publicKey}")
    public ResultMsg<String> publicKey(@PathVariable("publicKey") String publicKey) {
        return ResultMsg.resultSuccess(sysPublicParamService.getSysPublicParamKeyToValue(publicKey));
    }

    /**
     * 分页查询
     *
     * @param page
     * @param param
     * @return
     */
    @Operation(
            summary = "分页查询",
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @GetMapping("/page")
    public ResultMsg<IPage<SysPublicParam>> pageParams(@ParameterObject Page<SysPublicParam> page,@ParameterObject SysPublicParam param) {
        return ResultMsg.resultSuccess(sysPublicParamService.page(page, Wrappers.query(param)));
    }

    /**
     * 更新
     * @param param
     * @return
     */
    @Operation(
            summary = "更新公共参数",
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @PutMapping
    @SysLog(value = "更新参数",businessType = LogBusType.MODIFY)
    @PreAuthorize("hasAnyAuthority('sys_publicparam_edit')")
    public ResultMsg<SysPublicParam> update(@Parameter @RequestBody @Valid SysPublicParam param){
        sysPublicParamService.updateById(param);
        return ResultMsg.resultSuccess(param);
    }

    /**
     * 新增
     * @param param
     * @return
     */
    @Operation(
            summary = "新增公共参数",
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @PostMapping
    @SysLog(value = "新增参数",businessType = LogBusType.ADD)
    @PreAuthorize("hasAnyAuthority('sys_publicparam_add')")
    public ResultMsg<SysPublicParam> add(@Parameter @RequestBody SysPublicParam param){
        sysPublicParamService.save(param);
        return ResultMsg.resultSuccess(param);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Operation(
            summary = "删除公共参数",
            parameters = {@Parameter(name = "id",description = "公共参数id",required = true,in = ParameterIn.PATH)},
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @DeleteMapping("{id}")
    @SysLog(value = "删除参数",businessType = LogBusType.DELETE)
    @PreAuthorize("hasAnyAuthority('sys_publicparam_del')")
    public ResultMsg<Boolean> del(@PathVariable("id") Long id){
        return ResultMsg.resultSuccess(sysPublicParamService.removeParam(sysPublicParamService.getById(id)));
    }

    @Operation(
            summary = "刷新缓存",
            parameters = {@Parameter(name = "key",description = "key值",required = true,in = ParameterIn.QUERY)},
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @DeleteMapping("sync")
    @SysLog(value = "刷新缓存",businessType = LogBusType.DELETE)
    @PreAuthorize("hasAnyAuthority('sys_publicparam_sync')")
    public ResultMsg<Boolean> sync(@RequestParam(value = "key") String key){
        return ResultMsg.resultSuccess(sysPublicParamService.sync(key));
    }
}
